﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using SharpMap.Rendering;
using System.Windows.Data;
using System.ComponentModel;
using SharpMap.Wpf.Rendering.Styles;
using Util = SharpMap.Wpf.Rendering.Utility;
namespace SharpMap.Wpf.Rendering
{
    public class SymbolVisual : GeoVisual
    {
        //PointStyle _style = new PointStyle();
        DrawingVisual _theVisual = new DrawingVisual();

        public Point Location
        {
            //get { return (Point)GetValue(LocationProperty); }
            //set { SetValue(LocationProperty, value); }
            get { return DrawingRect.Location; }
            set { DrawingRect = new Rect(value,DrawingRect.Size); }
        }

  
        
        public override Rect Bounds
        {
            get { return DrawingRect; }
        }


        private Rect DrawingRect
        {
            get { return (Rect)GetValue(DrawingRectProperty); }
            set { SetValue(DrawingRectProperty, value); }
        }

        static readonly DependencyProperty DrawingRectProperty =
            DependencyProperty.Register("DrawingRect", typeof(Rect), typeof(GeoVisual));



        public ImageDrawing Drawing
        {
            get { return (ImageDrawing)GetValue(DrawingProperty); }
            set { SetValue(DrawingProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Drawing.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DrawingProperty =
            DependencyProperty.Register("Drawing", typeof(ImageDrawing), typeof(SymbolVisual));




        public SymbolVisual()
        {
            //AddVisualChild(_theVisual);
            Children.Add(_theVisual);
            VisualTransform = new MatrixTransform();
            DrawingRect = new Rect();
            Drawing = new ImageDrawing();

            RenderOptions.SetEdgeMode(Drawing, EdgeMode.Aliased);

            Binding b = new Binding("DrawingRect");
            b.Source = this;
            BindingOperations.SetBinding(Drawing, ImageDrawing.RectProperty, b);
        }
 
       public override void Render(WpfStyleHolder style)
        {
            PointStyle ngStyle = style as PointStyle;
              if (ngStyle == null)
                  return;
             if (ngStyle.ImageSource == null)
             {
                 return;
             }
             PropertyDescriptor propdes = DependencyPropertyDescriptor.FromProperty(Util.ScaleInvariantBase.InversionScaleProperty, style.GetType());
            //propdes.AddValueChanged(style,InversionScaleChanged);

            Drawing.ImageSource = ngStyle.ImageSource;
            DrawingRect = new System.Windows.Rect(this.Location,ngStyle.Size);
            RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
              using (DrawingContext ctx = this.RenderOpen())
            {
                ctx.DrawDrawing(Drawing);
            }
         }
  

        void InversionScaleChanged(object sender,EventArgs e)
        {
            PointStyle ps = sender as PointStyle;
            //DrawingRect = ps.ScaledRect;
        }
    }
}
